166. 分数到小数
为保证权益,题目请参考 166. 分数到小数(From LeetCode).
解决方案1
CPP
C++
//
// Created by lenovo on 2020-09-30.
//
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class Solution {
public:
string fractionToDecimal(long long numerator, long long denominator) {
int sign = 1;
if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0)) {
sign = -1;
numerator = numerator > 0 ? numerator : -numerator;
denominator = denominator > 0 ? denominator : -denominator;
}
string ans = "";
long long a = 0;
long long b = 0;
a = numerator / denominator;
b = numerator % denominator;
ans += to_string(a);
ans += b == 0 ? "" : ".";
unordered_map<int, int> aHistory;
aHistory.emplace(numerator, -1);
int position = to_string(a).length() + 1;
while (b) {
b = b * 10;
if (aHistory.find(b) != aHistory.end()) {
ans.insert(aHistory[b], "(");
ans += ")";
break;
}
aHistory.emplace(b, position);
position += 1;
a = b / denominator;
b = b % denominator;
ans += to_string(a);
}
ans = (sign == 1 ? "" : "-") + ans;
return ans;
}
};
void runTest() {
Solution so;
cout << so.fractionToDecimal(1, 3);
}
int main() {
runTest();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Python
python
# 166. 分数到小数
# https://leetcode-cn.com/problems/fraction-to-recurring-decimal/
################################################################################
class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
isNegative = False
if (numerator < 0 and denominator > 0) or (numerator > 0 and denominator < 0):
isNegative = True
numerator = -numerator if numerator < 0 else numerator
denominator = -denominator if denominator < 0 else denominator
intergerNum, left = divmod(numerator, denominator)
if left == 0:
if intergerNum == 0:
return str(intergerNum)
else:
if isNegative:
return str(-intergerNum)
else:
return str(intergerNum)
a = left
b = denominator
d = dict()
decimals = []
targetIndex = -1
for i in range(10005):
if a == 0:
break
a = a * 10
if a in d:
targetIndex = d[a]
break
else:
d[a] = i
c, e = divmod(a, b)
decimals.append(str(c))
a = e
if targetIndex != -1:
decimals.insert(targetIndex, "(")
decimals.append(")")
if isNegative:
return "-" + str(intergerNum) + "." + "".join(decimals)
else:
return str(intergerNum) + "." + "".join(decimals)
################################################################################
if __name__ == "__main__":
solution = Solution()
print(solution.fractionToDecimal(27, 12))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59